package com.base.sms.service.notice;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.lang.StringUtils;
import com.base.sms.config.SMSConfig;
import com.base.sms.handler.factory.SendHandlerFactory;
import com.base.sms.model.enums.SMSHandlerEnum;
import com.base.sms.model.query.NoticeQuery;
import com.base.sms.exception.NotFindSendHandlerException;
import com.base.sms.handler.SendHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 短信通知服务实现
 *
 * @author becker
 */
@Service
public class DefaultNoticeService implements NoticeService, InitializingBean {

    private static final Logger log = LoggerFactory.getLogger(DefaultNoticeService.class);

    private SMSConfig config;

    private SendHandler sendHandler;

    @Autowired
    private SendHandlerFactory sendHandlerFactory;

    /**
     * Invoked by the containing {@code BeanFactory} after it has set all bean properties
     * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
     * <p>This method allows the bean instance to perform validation of its overall
     * configuration and final initialization when all bean properties have been set.
     *
     * @throws Exception in the event of misconfiguration (such as failure to set an
     *                   essential property) or if initialization fails for any other reason
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        //默认使用爱信短信
        this.sendHandler = sendHandlerFactory.newInstance(SMSHandlerEnum.AIXUN.type());
    }

    @Override
    public boolean phoneRegValidation(String phone) {
        return StringUtils.isNotBlank(phone) && (StringUtils.isBlank(config.getRegx()) || phone
                .matches(config.getRegx()));
    }

    @Override
    public boolean send(NoticeQuery noticeQuery, Collection<String> phones) {
        if (ObjectUtil.isEmpty(noticeQuery)) {
            log.debug("noticeData is null");
            return false;
        }

        if (phones == null || phones.isEmpty()) {
            log.debug("phones is empty");
            return false;
        }

        List<String> phoneList = phones.stream().filter(this::phoneRegValidation).collect(Collectors.toList());

        if (phoneList.isEmpty()) {
            log.debug("after filter phones is empty");
            return false;
        }
        if(StringUtils.isNotBlank(SMSHandlerEnum.getValue(noticeQuery.getType().toUpperCase()))){
            log.info("[短信类型]:"+noticeQuery.getType());
            sendHandler = sendHandlerFactory.newInstance(SMSHandlerEnum.getValue(noticeQuery.getType().toUpperCase()));
        }
        if (ObjectUtil.isEmpty(sendHandler)) {
            throw new NotFindSendHandlerException();
        }
        return sendHandler.send(noticeQuery, phones);
    }

    @Autowired
    public void setConfig(SMSConfig properties) {
        this.config = properties;
    }

}